package com.biostime.qdingding.modules.cms.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.biostime.qdingding.common.config.Global;
import com.biostime.qdingding.common.persistence.Page;
import com.biostime.qdingding.common.utils.StringUtils;
import com.biostime.qdingding.common.web.BaseController;
import com.biostime.qdingding.modules.cms.entity.Article;
import com.biostime.qdingding.modules.cms.entity.Category;
import com.biostime.qdingding.modules.cms.service.ArticleService;
import com.biostime.qdingding.modules.cms.service.CategoryService;
import com.biostime.qdingding.modules.cms.service.ChannelService;
import com.biostime.qdingding.modules.qd.entity.Album;
import com.biostime.qdingding.modules.qd.service.AlbumService;
import com.biostime.qdingding.modules.sys.entity.User;
import com.biostime.qdingding.modules.sys.utils.UserUtils;

/**
 * 文章表Controller
 * @author king
 * @version 2015-12-11
 */
@Controller
@RequestMapping(value = "${adminPath}/cms/article")
public class ArticleController extends BaseController {

	@Autowired
	private ArticleService articleService;
	@Autowired
	private ChannelService channelService;
	@Autowired
	private CategoryService categoryService;
	@Autowired
	private AlbumService albumService;

	@ModelAttribute
	public Article get(@RequestParam(required=false) String id) {
		Article entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = articleService.get(id);
		}
		if (entity == null){
			entity = new Article();
		}
		return entity;
	}
	
	@RequiresPermissions("cms:article:view")
	@RequestMapping(value = {"list", ""})
	public String list(Article article, HttpServletRequest request, HttpServletResponse response, Model model) {
		String value = request.getParameter("value");
			if(!StringUtils.isEmpty(value)){
				model.addAttribute("value", value);
				article.setCategory(new Category("0"));
			}else if(article.getValue() != null && article.getValue().equals("pic")){
				model.addAttribute("value",value);
				article.setCategory(new Category("0"));
			}else if(request.getAttribute("value")!=null&&request.getAttribute("value").equals("pic")){
				model.addAttribute("value",value);
				article.setCategory(new Category("0"));
			}else{
				model.addAttribute("value", "article");
			}
		Page<Article> page = articleService.findPage(new Page<Article>(request, response), article); 
		model.addAttribute("page", page);
		return "modules/cms/articleList";
	}

	@RequiresPermissions("cms:article:view")
	@RequestMapping(value = "form")
	public String form(Article article,HttpServletRequest request, Model model) {
		if(article.getId() != null && article.getId() != "" ){
			Album ca = new Album();
			ca.setCategory("article");
			ca.setObjectId(StringUtils.toInteger(article.getId()));
			List<Album> articleAlbum = albumService.findList(ca);
			model.addAttribute("articleAlbum", articleAlbum);
		}
		String value = request.getParameter("value");
		if(value != null && value.equals("pic")){
			article.setIsSlide(1);
			model.addAttribute("value", value);
		}else{
			article.setIsSlide(0);
			model.addAttribute("value", "article");
		}
		
		int center = StringUtils.toInteger(UserUtils.getUser().getCenter().getId());
		
		List<Category> categoryList = categoryService.getParentsTree();
		model.addAttribute("categoryList", categoryList);
		model.addAttribute("center", center);
		model.addAttribute("article", article);
		return "modules/cms/articleForm";
	}

	@RequiresPermissions(value={"cms:article:edit","cms:article:add"}, logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(Article article, Model model, HttpServletRequest request, RedirectAttributes redirectAttributes ,HttpServletResponse response) {
		User curUser = UserUtils.getUser();
		
		article.setCenter(curUser.getCenter());
		article.setChannelId(0);
		article.setStatus(0);
		articleService.save(article);
		
		Album cla = new Album();
		cla.setCategory("article");
		cla.setObjectId(StringUtils.toInteger(article.getId()));
		albumService.deleteAll(cla);
		
		String[] photos = request.getParameterValues("hid_photo_name");
		String[] remarks = request.getParameterValues("hid_photo_remark");
		if(photos!=null){
			for(int i=0;i<photos.length;i++){
				String photo = photos[i];
				String remark = remarks[i];
				
				if(!StringUtils.isEmpty(photo)){
					String[] parts = photo.split("\\|");
					if(parts!=null && parts.length>=6){
						Album album = new Album();
						album.setObjectId(StringUtils.toInteger(article.getId()));
						album.setCategory(parts[0]);
						album.setFileName(parts[1]);
						album.setOrginPath(parts[2]);
						album.setThumbPath(parts[3]);
						album.setAlbumSize(StringUtils.toInteger(parts[4]));
						album.setRemarks(remark);
						
						albumService.save(album);
					}
				}
			}
		}
		addMessage(redirectAttributes, "保存成功");
		if(article.getValue() != null && article.getValue().equals("pic")){
			return "redirect:"+Global.getAdminPath()+"/cms/article/?repage&value=pic";
		}
		return "redirect:"+Global.getAdminPath()+"/cms/article/?repage";
	}
	
	@RequiresPermissions("cms:article:edit")
	@RequestMapping(value = "delete")
	public String delete(@RequestParam(value="ids",required=false) String idsRequest, Article article, RedirectAttributes redirectAttributes) {
		//批量删除
		if(!StringUtils.isEmpty(idsRequest)){
			String[] ids = idsRequest.split(",");
			for(String id : ids){
				articleService.delete(new Article(id));
			}
		}else{
			articleService.delete(article);
		}
		
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/cms/article/?repage";
	}
}